package com.gewei.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.gewei.constant.MessageConstant;
import com.gewei.entity.PageResult;
import com.gewei.entity.QueryPageBean;
import com.gewei.entity.Result;
import com.gewei.pojo.CheckGroup;
import com.gewei.pojo.CheckItem;
import com.gewei.pojo.Member;
import com.gewei.pojo.Setmeal;
import com.gewei.service.MemberService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Reference
    private MemberService memberService;

    @RequestMapping("/add")
    public Result add(@RequestBody Member member) {
        try {
            memberService.add(member);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
        return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = memberService.pageQuery(queryPageBean);
        return pageResult;
    }


    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            memberService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
    }


    @RequestMapping("/edit")
    public Result edit(@RequestBody Member member) {
        try {
            memberService.edit(member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
    }


    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Member member = memberService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        List<Member> memberList = memberService.findAll();
        if (memberList != null && memberList.size() > 0) {
            Result result = new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS);
            result.setData(memberList);
            return result;
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }

    @RequestMapping("/exportMemberExcel")
    public Result exportMemberExcel(HttpServletRequest request, HttpServletResponse response) {
        List<Member> memberList = memberService.findAll();
        String filePath = request.getSession().getServletContext().getRealPath("template") + File.separator + "member_template.xlsx";
        try {
            XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(filePath)));//输入流
            XSSFRow row = null;
            XSSFSheet sheet = workbook.getSheetAt(0);
            for (int i = 0; i < memberList.size(); i++) {
                row = sheet.createRow(i + 1);
                String name = memberList.get(i).getName();
                XSSFCell cell = row.createCell(1);
                cell.setCellValue(name);
                cell = row.createCell(2);
                cell.setCellValue(memberList.get(i).getPhoneNumber());
                ArrayList<Setmeal> setmeals = memberList.get(i).getSetmeals();
                ArrayList<String> setmealnames = new ArrayList<>();
                ArrayList<String> checkGroupnames = new ArrayList<>();
                ArrayList<String> checkItemnames = new ArrayList<>();
                for (Setmeal setmeal : setmeals) {
                    setmealnames.add(setmeal.getName());
                    List<CheckGroup> checkGroups = setmeal.getCheckGroups();
                    System.out.println(setmeal.getName());
                    for (CheckGroup checkGroup : checkGroups) {
                        checkGroupnames.add(checkGroup.getName());
                        System.out.println(checkGroup.getName());
                        for (CheckItem checkItem : checkGroup.getCheckItems()) {
                            checkGroupnames.add(checkItem.getName());
                            System.out.println(checkItem.getName());
                        }
                    }
                }
                cell = row.createCell(3);
                cell.setCellValue(setmealnames.toString());
                cell = row.createCell(4);
                cell.setCellValue(checkGroupnames.toString());
                cell = row.createCell(5);
                cell.setCellValue(checkItemnames.toString());
            }
            ServletOutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");//代表的是Excel文件类型
            response.setHeader("content-Disposition", "attachment;filename=report.member.xlsx");//指定以附件形式进行下载
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        }


      /*  try {
            String filePath = request.getSession().getServletContext().getRealPath("template") + File.separator + "member_template.xlsx";
            OutputStream out = response.getOutputStream();

            XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(filePath)));//输入流

            response.setContentType("application/vnd.ms-excel");//代表的是Excel文件类型
            response.setHeader("content-Disposition", "attachment;filename=report.member.xlsx");//指定以附件形式进行下载
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        return null;
    }
}